package com.feishi.core.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import static org.apache.lucene.document.StringField.TYPE_STORED;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-4-16 09:46
 **/
public class CreateIndex {

    public static void main(String[] args) throws IOException {
        //定义IndexWriter

        String projectPath=IndexFiles.class.getResource("/").getPath();
        //索引文件地址
        Path path = FileSystems.getDefault().getPath(projectPath, "index");
        Directory directory = FSDirectory.open(path);
        //定义分词器
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer).setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

        //定义文档
        Document document = new Document();
        //定义文档字段
//        document.add(new LegacyLongField("id", 5499, Field.Store.YES));
        document.add(new Field("title", "小米6", TYPE_STORED));
        document.add(new Field("sellPoint", "骁龙835，6G内存，双摄！", TYPE_STORED));
        //写入数据
        indexWriter.addDocument(document);
        //添加新的数据
        document = new Document();
//        document.add(new LongField("id", 8324, Field.Store.YES));
        document.add(new Field("title", "OnePlus5", TYPE_STORED));
        document.add(new Field("sellPoint", "8核，8G运行内存", TYPE_STORED));
        indexWriter.addDocument(document);
        //提交
        indexWriter.commit();
        //关闭
        indexWriter.close();

    }


}
